//
// File: clientmanager.cpp
//

#include "clientmanager.h"

Client::Client() {}
Client::Client(int id, const string name, NetworkPeer peer) 
	: id(id), name(name), peer(peer) 
{
	cout << peer << " connected as " << name << endl;
}

void Client::process(void *data) {
	cout << name << " got some data." << endl; 
}

void Client::disconnect() {
	cout << name << " disconnected." << endl; 
}


//
// Associate a peer with a client session
ClientManager::ClientManager() : nextClient(0) {
	
}

bool ClientManager::add(NetworkPeer p) {
	ostringstream ss;
	ss << "#" << nextClient++ << "#";
	clients[p] = new Client(nextClient, ss.str(), p);
}

bool ClientManager::drop(NetworkPeer p) {
	
	// check if client exists
	map<NetworkPeer, Client*>::iterator i = clients.find(p);
	if (i == clients.end()) {
		return false;
	}
	
	Client *c = i->second;
	clients.erase(i);
	delete c;
	
	return true;
}

void ClientManager::process(NetworkPeer p, void *data) {
	cout << p << " got some data" << endl;
}
